import utils from "../../utils/index";
import {
  fansModel
} from "../../apis/index"
const app = getApp()
import regeneratorRuntime from '../../libs/wxPromise.min';
import {
  InterAd
} from '../../utils/InterAd'
Page({

  data: {
    keywords:'',
    page: {
      page: 1,
      pageSize: 10
    },
    list: [],
    placeholder:'输入明星名字',
    btnText:'打榜',
    type:'1'
  },
  mixins: [require('../../myMixin.js')],
  onLoad: function (options) {
    let text = options.text?options.text:'打榜'
    this.data.type = options.type?options.type:'1' //1为打榜  2 选中数据回传
    this.setData({
      btnText:text
    })
  },
  onReady: function () {
 

      let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
      interAd.showAd()
  
  },
  onShow: function () {

  },
  getSearchList() {
    let {
      page,
      pageSize
    } = this.data.page
    let { keywords,list } = this.data

    let param = {
      page: page,
      pageSize: pageSize,
      name: keywords.trim()
    }
    utils.showLoading()
    fansModel.searchIdols(param).then(res => {
      let _list = res.list
      list = list.concat(_list)
      this.computedPage('page', res.total)
      this.setData({
        list: list
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  moreListData() {
    this.loadMoreData('page', this.getSearchList)
  },
  onInputChange(e){
    let value = e.detail.value
    this.setData({
      keywords:value
    },()=>{
      this.searchHandle()
    })
  },
  searchHandle(){
    let {keywords} = this.data
    if(!keywords.trim()){

      return
    }
    this.timer = this.timer || null
    clearTimeout(this.timer)
    this.timer = setTimeout(()=>{
      this.data.list = []
      this.data.page.page = 1
      this.getSearchList()
    },500)
  },
  clearKeyWords(){
    this.setData({
      keywords:'',
      list:[]
    })
  },
  tapHandler(e){
    let item = e.currentTarget.dataset.item
    let { type } = this.data

    if(type=='1'){ //
      utils.redirectTo({
        url: `../idol/index?sid=${item.id}`
      })
    }else if(type=='2'){ //数据回传
      app.globalData.search_data = item
      utils.navigateBack({
        delta:1
      })
     
    }
  }
})